package com.studio49.exmple.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 
 * <PRE>
 * 时间工具类
 * </PRE>
 */
public class PubDateUtils extends DateUtil {

	/**
	 * 得到今天的最早日期时间（00:00:00）
	 * 
	 * @return
	 */
	public static Date getTodayFirstTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 得到今天的最晚时间（23:59:59）
	 * 
	 * @return
	 */
	public static Date getTodayLastTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 得到本周周一的日期时间（00:00:00）
	 * 
	 * @return
	 */
	public static Date getWeekFirstDayTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 得到本周周日最后时间日期(23:59:59)
	 * 
	 * @return
	 */
	public static Date getWeekLastDayTime() {
		Calendar calendar = Calendar.getInstance();
		// 这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		// 增加一个星期，才是我们中国人理解的本周日的日期
		calendar.add(Calendar.WEEK_OF_YEAR, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取本月的第一天日期 最后时间(23:59:59)
	 * 
	 * @return
	 */
	public static Date getMonthFirstDayLastTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取本月的第一天日期时间(00:00:00)
	 * 
	 * @return
	 */
	public static Date getMonthFirstDayTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date date = calendar.getTime();
		return date;
	}
	
	/**
	 * 获取本月的最后一天日期时间(23:59:59)
	 * 
	 * @return
	 */
	public static Date getMonthLastDayTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取当前系统dayOfMoth日期
	 * 
	 */
	public static Date getDayTime(int dayOfMoth) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getNow());
		calendar.add(Calendar.DAY_OF_MONTH, dayOfMoth);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Date getNow() {
		return new Date();
	}

	/**
	 * 
	 * 验证currentDate日期是否在开始区间sSection-结束区间eSection之间
	 * 
	 */
	public static boolean checkDateSection(Date currentDate, Date sSection, Date eSection) {
		boolean r = false;
		if (currentDate.getTime() >= sSection.getTime() && currentDate.getTime() <= eSection.getTime()) {
			r = true;
		}
		return r;
	}

	/**
	 * 时间和时间区间的比较
	 * 
	 * @param time
	 *            时间
	 * @param sSection
	 *            时间区间开始时间
	 * @param eSection
	 *            时间区间结束时间
	 * @return 时间小于时间区间返回 -1,时间在时间区间内返回0,时间大于时间区间返回1
	 */
	public static int timeCompareTimeInterval(Date time, Date sSection, Date eSection) {
		if (sSection.getTime() >= eSection.getTime()) {
			throw new IllegalArgumentException("不能满足条件: 时间区间结束时间>时间区间开始时间 ");
		}
		if (time.getTime() < sSection.getTime()) {
			return -1;
		}
		if (time.getTime() > eSection.getTime()) {
			return 1;
		}
		return 0;
	}

	public static String getFormatDate(String dateStr, int day, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date date = sdf.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, day);
			return sdf.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 日期转cron表达式<定时器时间格式>
	 * 
	 * @param date
	 * @return
	 */
	public static String timeToCron(Date date) {
		String dateFormat = "ss mm HH dd MM ? yyyy";
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		String formatTimeStr = null;
		if (date != null) {
			formatTimeStr = sdf.format(date);
		}
		return formatTimeStr;
	}

	/**
	 * 时间相减得到天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int getDaySub(Date beginDate, Date endDate) {
		int day = 0;
		try {
			String pattern = "yyyy-MM-dd";
			String beginDateStr = DateUtil.dateToString(beginDate, pattern);
			Date beginDate1 = DateUtil.stringToDate(beginDateStr,pattern);
			String endDateStr = DateUtil.dateToString(endDate, pattern);
			Date endDate1 = DateUtil.stringToDate(endDateStr,pattern);
			
			double aa = endDate1.getTime() - beginDate1.getTime();
			double day_d = aa / (24 * 60 * 60 * 1000);
			day = (int) Math.round(day_d);
			// System.out.println("相隔的天数="+day);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * 获取日程循环时间的开始时间
	 * @param time
	 * @return
	 */
	public static Date getRegularStartTime(String time){
		String[] times = time.split(":");
		if(times.length < 3) {
			return new Date();
		}
		//时间点
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(times[0]));
		calendar.set(Calendar.MINUTE, Integer.valueOf(times[1]));
		calendar.set(Calendar.SECOND, Integer.valueOf(times[2]));
		//现在
		Calendar now = Calendar.getInstance();
		if(now.compareTo(calendar) > 0) {
//			date = calendar.getTime();
		}else {
			calendar.add(Calendar.DAY_OF_MONTH, -1);
		}
		Date date = calendar.getTime();
		return date;
	}
	/**
	 * 获取日程循环时间的截止时间
	 * @param time
	 * @return
	 */
	public static Date getRegularEndTime(String time){
		String[] times = time.split(":");
		if(times.length < 3) {
			return new Date();
		}
		//时间点
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(times[0]));
		calendar.set(Calendar.MINUTE, Integer.valueOf(times[1]));
		calendar.set(Calendar.SECOND, Integer.valueOf(times[2]));
		//现在
		Calendar now = Calendar.getInstance();
		if(now.compareTo(calendar) > 0) {
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}
		Date date = calendar.getTime();
		return date;
	}
	
	
	// public static void main(String[] args){
	// // Date date1 = getTodayFirstTime();
	// // Date date2 = getTodayLastTime();
	// // Date date3 = getMonthFirstDayTime();
	// // Date date4 = getMonthLastDayTime();
	// // System.out.println(DateUtil.dateToString(date1) );
	// // System.out.println(DateUtil.dateToString(date2));
	// // System.out.println(DateUtil.dateToString(date3));
	// // System.out.println(DateUtil.dateToString(date4));
	// String cron=getCorn(new Date());
	// System.out.println(cron);
	// }
	
}
